import axios from '@/utils/request.js'
import moment from 'moment';
let onlineEndTime = moment().format('YYYY-MM-DD HH:mm:ss');
const sendLogoutRequest = async (url,account,loginDate,onlineStartTime) => {
    try {
      const response = await axios.post(url, {
        account: account,
        loginDate: loginDate,
        onlineStartTime: onlineStartTime,
        onlineEndTime: onlineEndTime
      });
      if (response.data.code === 1) {
        console.log('员工下线成功');
      } else {
        console.error('员工下线请求失败，状态码:', response.status);
      }
    } catch (error) {
      console.error('发送员工下线请求时发生错误:', error.message);
    }
  };
//下线之后  
export const monitorOffLine = (url,account,loginDate,onlineStartTime) => {
    let timeout;
    const handleBeforeUnload = () => {
      timeout = setTimeout(() => {
        sendLogoutRequest(url,account,loginDate,onlineStartTime);
        //移除请求头
        removeToken();
      }, 5000);
    };
    const handleVisibilityChange = () => {
      if (document.visibilityState === 'hidden') {
        timeout = setTimeout(() => {
          sendLogoutRequest(url,account,loginDate,onlineStartTime);
          //移除请求头
          removeToken();
        }, 5000);
      } else {
        clearTimeout(timeout);
      }
    };
    window.addEventListener('beforeunload', handleBeforeUnload);
    document.addEventListener('visibilitychange', handleVisibilityChange);
    return () => {
      window.removeEventListener('beforeunload', handleBeforeUnload);
      document.removeEventListener('visibilitychange', handleVisibilityChange);
      clearTimeout(timeout);
    };
  };  